<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import delIconBg from "assets/images/del-icon-bg.png";
    import editIconBg from "assets/images/edit-icon-bg.png";
    import uncheckedIconBg from "assets/images/unchecked-circle.png";
    import checkedIconBg from "assets/images/checked-circle.png";
    import newAddressIcon from "assets/images/new-address-icon.png";

    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    const globalEvent = weex.requireModule('globalEvent');

    export default{
        data(){
            return {
                delIconBg: delIconBg,
                editIconBg: editIconBg,
                uncheckedIconBg: uncheckedIconBg,
                checkedIconBg: checkedIconBg,
                newAddressIcon: newAddressIcon,
                listLoaded: false,
                addrList : [],
                defaultAddrId: null
            }
        },
        created() {
            let self = this;
            self.getAddrList(res=> {
                 self.listLoaded = true;
            });

            //监听新增或编辑收货地址事件
            globalEvent.addEventListener(constant.EVENT_EDIT_ADDRESS, function (e) {
                self.getAddrList();
            });
        },
        methods: {
            getAddrList(callback){
                let self = this;
                api.sendHttp("getUserAddressListApi",{}).then(res=>{
                    self.addrList = res.data || [];
                    self.addrList.map((v,i,arr) =>{
                        if(v.addr_default == 1) {
                            self.defaultAddrId = v.addr_id;
                            return false;
                        }
                    });

                    if(typeof callback == 'function') callback(res);
                },
                res=>{
                    modal.toast(res.msg || "获取收货地址失败");
                });
            },
            setDefault(addr){
                let self = this;
                if(self.defaultAddrId == addr.addr_id){
                    return;
                }

                api.sendHttp("setDefaultAddressApi",{id: addr.addr_id}).then(res => {
                    modal.toast("设置成功");
                    self.removeDefault(self.defaultAddrId);
                    self.defaultAddrId = addr.addr_id;
                    self.sortDefault(addr);
                },
                res => {
                    modal.toast(res.msg || "设置默认收货人地址失败");
                });

            },
            newAdrr(){
                let self = this;
                let options = {
                    classType: constant.Pop_Class_Type,
                    url: route.address_edit,
                    title: '新建收货地址'
                };
                navigator.push(options);
            },
            editAddr(addr){
                let self = this;
                let options = {
                    classType: constant.Pop_Class_Type,
                    url: route.address_edit,
                    title: '编辑收货地址',
                    params: {
                        address: addr
                    }
                };
                 navigator.push(options);
            },
            delAdrr(addr){
                let self = this;
                modal.confirm({message: "确认删除该收货地址吗?"}).then(() => {
                    api.sendHttp("delUserInfoApi",{id: addr.addr_id}).then(res =>{
                        self.delAddrList(addr);
                    },
                    res =>{
                        modal.toast(res.msg || "删除收货地址失败");
                    });
                });
            },
            removeDefault(addr_id){
                let self = this;
                if(addr_id) {
                    self.addrList.map((v,i,arr) =>{
                        if(addr_id == v.addr_id) {
                            v.addr_default = 0;
                            self.addrList.$set(i,v);
                        }
                    });
                }
            },
            sortDefault(addr){
                let self = this;
                addr = addr || {};
                self.addrList.map((v,i,arr) =>{
                    if(addr.addr_id == v.addr_id) {
                        addr.addr_default = 1;
                        self.addrList.splice(i,1);
                        self.addrList.unshift(addr);
                    }
                });
            },
            delAddrList(addr){
                let self = this;
                if(addr && addr.addr_id) {
                    self.addrList.map((v,i,arr) =>{
                        if(addr.addr_id == v.addr_id) {
                            self.addrList.splice(i,1);
                        }
                    });
                }
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_EDIT_ADDRESS);
        }
    }
</script>
